<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Users;
class sign extends Common
{
    //每日签到列表接口
   
    public function signList()
    {
        $uid = Request::instance()->post('uid','');//获取登录用户id
        if(empty($uid)){
          return json(['state'=>'203','msg'=>'登录用户为空']);
        }else{
          $rs=Db::name('user_info')->where(['uid'=>$uid,'userFlag'=>1])->find();
            if($rs){
              $rd=Db::name('user_daily')->where(['uid'=>$uid,'create_time'=>date("Y-m-d")])->find();
              if($rd){
                   return json(['state'=>'204','msg'=>'今天已签到过']);
              }
             //签到时间
               $user_daily=Db::name('user_daily')->field('sign_days')->where(['uid'=>$uid])->find();
               if(empty($user_daily)){
                 $user_daily['sign_days']=0;
               }
                //七天的关注劵
             $coupon=Db::name('coupon')->field('id,couponName')->select();
             if($coupon){
               $result=['user_daily'=>$user_daily,'coupon'=>$coupon];
              return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
             }else{
              return json(['state'=>'201','msg'=>'查询内容不存在']);
             }
            }else{
              return json(['state'=>'202','msg'=>'用户已不存在']);
            }
           
        }
        
    }
    //签到领取接口
    public function signDraw(){
	 $uid = Request::instance()->post('uid','');//用户id
	 $aid = Request::instance()->post('aid','');//签到天数
    if(empty($uid)){
      return json(['state'=>'202','msg'=>'用户为空']);
     }else{
    	    $re=Db::name('user_info')->where(['uid'=>$uid])->field('uid')->find();
    		 if(empty($re)){
              return json(['state'=>'204','msg'=>'用户已不存在']);
    		}else{
    		 $rd=Db::name('user_daily')->where(['uid'=>$uid,'create_time'=>date("Y-m-d")])->find();
        // echo $rd;die;
    		 if($rd){
               return json(['state'=>'205','msg'=>'今天已签到过']);
    		 }else{
          //判断签到是不是今天的
           $user_daily=Db::name('user_daily')->where(['uid'=>$uid])->find();
            if(empty($user_daily) && $aid!=1){
              return json(['state'=>'206','msg'=>'请签到今天的']);
            }
          $sdata=['uid'=>$uid,'cpnid'=>$aid,'create_time'=>date("Y-m-d H:i:s")];
          //当前时间
          $nowTime=strtotime(date("Y-m-d"));
         // echo $nowTime;die;
          //查询上次的签到时间
          $user_daily=Users::userSign($uid);

          if(empty($user_daily)){
             Db::startTrans();//开启事务
             try{
              //添加领取的关注劵
               Db::name('user_coupon')->insert($sdata);
               //添加连续签到数
               $sdata1=['uid'=>$uid,'sign_days'=>'1','create_time'=>date("Y-m-d")];
               Db::name('user_daily')->insert($sdata1);  
                // 提交事务
                Db::commit();    
                return  json(['state'=>'200','msg'=>'签到成功']);   
              }catch (\Exception $e){
                // 回滚事务
               Db::rollback();
                return  json(['state'=>'205','msg'=>'签到失败']);  
               }
          }
          $daily_time=$user_daily['create_time'];
          $uoTo=strtotime($daily_time);
          //时间差
          $daily_num=($nowTime-$uoTo)/86400;
         // echo $daily_num;die;
          Db::startTrans();//开启事务
          if($daily_num==1 && $user_daily['sign_days']<7){
            $sdata1=['uid'=>$uid,'cpnid'=>$aid,'create_time'=>date("Y-m-d H:i:s")];
            $data3=['create_time'=>date('Y-m-d')];
             try{
              //添加领取的关注劵
               Db::name('user_coupon')->insert($sdata1);
               //添加连续签到数
               Db::name('user_daily')->where(['uid'=>$uid])->setInc('sign_days');  
               Db::name('user_daily')->where(['uid'=>$uid])->update($data3);  
                // 提交事务
                Db::commit();    
                return  json(['state'=>'200','msg'=>'签到成功']);   
              }catch (\Exception $e){
                // 回滚事务
               Db::rollback();
                return  json(['state'=>'205','msg'=>'签到失败']);  
               }
              }else{
                 $sdata2=['uid'=>$uid,'cpnid'=>'1','create_time'=>date("Y-m-d")];
                 try{
              //添加领取的关注劵
               Db::name('user_coupon')->insert($sdata2);
               //添加连续签到数
               $data1=['sign_days'=>'1','create_time'=>date('Y-m-d')];
               Db::name('user_daily')->where(['uid'=>$uid])->update($data1);  
                // 提交事务
                Db::commit();    
                return  json(['state'=>'200','msg'=>'签到成功']);   
              }catch (\Exception $e){
                // 回滚事务
               Db::rollback();
                return  json(['state'=>'205','msg'=>'签到失败']);  
               }
              }
             }   
           }
    		  
    		}
    	 }

    }
